﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZLDC.Common.Estate;

namespace ZLDC.Estate
{
    public class CHandlerLoad
    {
        public static void ChooseMethod()
        {

            string act = _Request.GetString("act", "");

            switch (act)
            {
                case "loadhousing_list":
                    LoadHousingList();
                    break;
                case "loadhousing_detail":
                    LoadHousingDetail();
                    break;
                case "loadhousing_save":
                    LoadHousingSave();
                    break;

                #region 管理员找回被抢盘源

                case "NewSystemBuild_Auto":
                    NewSystemBuild_Auto();
                    break;

                case "OldSystemBuild_Auto":
                    OldSystemBuild_Auto();
                    break;

                case "system_loadhousing_list":
                    SystemLoadHousingList();
                    break;

                case "ok_loadhousing_list":
                    OkLoadHousingList();
                    break;

                #endregion

                #region 盘源匹配

                case "NewSystemEstate_Auto":
                    NewSystemEstate_Auto();
                    break;

                case "OldSystemEstate_Auto":
                    OldSystemEstate_Auto();
                    break;

                case "build_lastlist_byeid":
                    BuildLastlistByeid();
                    break;

                case "build_oldsystem_byeid":
                    BuildOldSystemByeid();
                    break;

                case "housing_match":
                    HousingMatch();
                    break;

                case "estate_isvalid_update":
                    EstateIsvalidUpdate();
                    break;

                case "housing_revoke":
                    HousingRevoke(); break;

                case "estate_allCancel":
                    EstateAllCancel(); break;

                case "oprAllProperty_list":
                    OprAllPropertyList(); break;

                case "getbidbyptyid":
                    GetBidByPtyID(); break;


                #endregion

            }
        }

        private static void LoadHousingList()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string station = _Utility.GetInfoBySession().m_sStation;
            string orderByName = _Request.GetString("orderByName", "pty_state");
            string orderByType = _Request.GetString("orderByType", "desc");

            string condition = " where isload=0 ";

            if (_Utility.GetWebConfigSetting("SalesManagerStation").Contains(station))
            {
                string deptid_list = CacheUtility.Cache_GetAllChildDept(_Utility.GetInfoBySession().m_sDeptID);
                condition += " and makedept_id in(" + deptid_list + ")";
            }
            else
            {
                condition += " and make_id =" + userid;
            }

            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_opr_AllProperty_list_table(userid, page, pagesize, condition, orderByName, orderByType);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void LoadHousingDetail()
        {
            int id = _Request.GetInt("id", 0);

            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_opr_AllProperty_detail_table(id);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void LoadHousingSave()
        {
            int id = _Request.GetInt("id", 0);
            int roomid = _Request.GetInt("roomid", 0);
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            string username = _Utility.GetInfoBySession().m_sUserName;

            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_opr_AllProperty_save(userid, username, id, roomid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        #region 管理员找回被抢盘源

        private static void NewSystemBuild_Auto()
        {
            int top = 500;
            string estatename = _Request.GetString("q", "");
            string json = "";

            if (estatename != "" && estatename != "楼盘名称、拼音均可")
            {
                string condition = "where delflag = 0 and (finalName like '" + estatename + "%' or finalNamePy like '" + estatename + "%')";

                json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.NewSystemBuild_Auto(top, condition);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void OldSystemBuild_Auto()
        {
            int top = 500;
            string estatename = _Request.GetString("q", "");
            string json = "";

            if (estatename != "" && estatename != "楼盘名称、拼音均可")
            {
                string condition = "where buildDltFlag = 0 and (finalName like '" + estatename + "%' or finalNamePy like '" + estatename + "%')";

                json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.OldSystemBuild_Auto(top, condition);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void SystemLoadHousingList()
        {
            int newbuildid = _Request.GetInt("newbuildid", 0); int oldbuildid = _Request.GetInt("oldbuildid", 0);
            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_opr_AllProperty_list_system_table(newbuildid, oldbuildid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void OkLoadHousingList()
        {
            int id1 = 0;
            string outmsg = "";
            string[] IDList1 = _Utility.GetStringBySplit(_Request.GetString("idlist1"), '|'); string[] IDList2 = _Utility.GetStringBySplit(_Request.GetString("idlist2"), '|');
            if (IDList1 != null && IDList1.Length > 0)
            {
                for (int i = 0; i < IDList1.Length; i++)
                {
                    id1 = _Convert.ToInt(IDList1[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_opr_AllProperty_list_ok(id1, 1);
                }
            }

            int id2 = 0;
            if (IDList2 != null && IDList2.Length > 0)
            {
                for (int i = 0; i < IDList2.Length; i++)
                {
                    id2 = _Convert.ToInt(IDList2[i], 0);
                    outmsg = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_opr_AllProperty_list_ok(id2, 2);
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }


        #endregion

        #region 盘源匹配

        private static void NewSystemEstate_Auto()
        {
            int top = 500;
            string estatename = _Request.GetString("q", "");
            string json = "";

            if (estatename != "" && estatename != "楼盘名称、拼音均可")
            {
                string condition = "where delflag = 0 and (name like '" + estatename + "%' or namePy like '" + estatename + "%')";

                json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.NewSystemEstate_Auto(top, condition);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void OldSystemEstate_Auto()
        {
            int top = 500;
            string estatename = _Request.GetString("q", "");
            string json = "";

            if (estatename != "" && estatename != "楼盘名称、拼音均可")
            {
                string condition = "where DltFlag = 0 and (name like '" + estatename + "%' or namePy like '" + estatename + "%')";

                json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.OldSystemEstate_Auto(top, condition);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void BuildLastlistByeid()
        {
            string outmsg = "";
            int eid = _Request.GetInt("eid", 0);

            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_build_lastlist_byeid_table(eid, out outmsg);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void BuildOldSystemByeid()
        {
            int eid = _Request.GetInt("eid", 0);

            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.BuildOldSystem(eid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void HousingMatch()
        {
            int newbid = _Request.GetInt("newbid", 0);
            int oldbid = _Request.GetInt("oldbid", 0);

            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_opr_AllProperty_list_system_table(newbid, oldbid, 1);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void EstateIsvalidUpdate()
        {
            int eid = _Request.GetInt("eid", 0);

            string json = ZLDC.Topo.BLL.Estate.EstateBLL.EstateCheck(eid, 0, "全部匹配完成(" + _Utility.GetInfoBySession().m_sUserName + ")");

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void HousingRevoke()
        {
            int bid = _Request.GetInt("bid", 0);
            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_housing_revoke(bid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void EstateAllCancel()
        {
            int eid = _Request.GetInt("eid", 0);
            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.EstateAllCancel(eid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void OprAllPropertyList()
        {
            int neweid = _Request.GetInt("neweid", 0); int oldeid = _Request.GetInt("oldeid", 0); int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string condition = " where isload = 0 and e_id=" + oldeid;

            string orderByName = _Request.GetString("orderByName", "pty_state");
            string orderByType = _Request.GetString("orderByType", "desc");

            string json = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.OprAllPropertyList(neweid, oldeid, page, pagesize, condition, orderByName, orderByType);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void GetBidByPtyID()
        {
            int ptyid = _Request.GetInt("ptyid", 0);
            int newbid = ZLDC.Topo.BLL.Estate.OprAllPropertyBLL.p_GetBidByPtyID(ptyid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(newbid);
            HttpContext.Current.Response.End();
        }


        #endregion

    }
}
